<template>
  <div>
    <div class="login-container">
      <!-- ref 相当于 id，:model 表单数据对象, label-width 表单域标签的的宽度 -->
      <el-form ref="form" label-width="80px" class="login-form">
        <h2 class="login-title">金丰会员管理系统-注册</h2>
        <el-form-item label="帐号">
          <el-input
            placeholder="请输入帐号"
            v-model.trim="username"
            @blur="checkname"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" placeholder="请输入密码" v-model="password">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="go">登录</el-button>
          <el-button type="primary" @click="reg">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import usersApi from "../../api/usersApi";
export default {
  name: "reg",
  data() {
    return {
      username: "",
      password: "",
      ischeck: false,
    };
  },

  components: {},

  methods: {
    go() {
      this.$router.push("/login");
    },

    //功能：失去焦点验证用户名是否存在
    checkname() {
      // console.log("789");
      if (this.username) {
        //用户名不为空，才发送请求
        //验证用户名符合一定的规则:正则
        usersApi
          .checkName(this.username)
          .then((res) => {
            console.log(res, 899);
            if (res.data.flag) {
              //验证通过：允许注册
              this.ischeck = true;
              this.$message({
                type: "success",
                message: "可以注册哦",
                duration: 2000,
              });
            } else {
              //验证不通过
              this.ischeck = false;
              this.$message({
                type: "error",
                message: "该用户名很受欢迎，请换一个",
                duration: 2000,
              });
            }
          })
          .catch((err) => {
            // console.log("出错了"); //发送请求失败：服务器异常
            this.$message({
              type: "error",
              message: "服务器异常",
              duration: 2000,
            });
          });
      } else {
        this.$message({
          type: "error",
          message: "用户名不能为空",
          duration: 2000,
        });
      }
    },

    //功能：注册功能
    reg() {
      //用户名验证通过 + 用户名不能为空 + 密码不能为空
      if (this.ischeck && this.username && this.password) {
        //可以注册的
        let pass = this.$md5(this.password);
        // console.log(pass, "加密后");
        usersApi
          .reg(this.username, pass)
          .then((res) => {
            console.log(res, 990);
            if (res.data.flag) {
              //注册成功
              this.$message({
                type: "success",
                message: "注册成功",
                duration: 2000,
              });
              //跳到登录页
              this.$router.push({
                path: "/login",
                query: { username: this.username },
              });
            } else {
              //注册失败
              this.$message({
                type: "error",
                message: "注册失败",
                duration: 2000,
              });
            }
          })
          .catch((err) => {
            this.$message({
              type: "error",
              message: "服务器异常",
              duration: 2000,
            });
          });
      } else {
        this.$message({
          type: "error",
          message: "用户名或密码不能为空",
          duration: 2000,
        });
      }
    },
  },
};
</script>

<style scoped>
.login-form {
  width: 350px;
  /* 上下间隙150px, 左右自动 */
  margin: 160px auto;
  /*透明背景色*/
  background-color: rgb(255, 255, 255, 0.8);
  padding: 30px;
  /* 圆角 */
  border-radius: 20px;
}
.login-container {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/img/login.jpg");
  overflow: hidden;
}
/* 标题 */
.login-title {
  text-align: center;
  color: #303133;
}
</style>
